########################################################################
#  Pyfm - personal finance data-base
#  Copyright (C) 2007,8 Ehud Ben-Reuven
#  udi@benreuven.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation version 2.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
########################################################################
"""Expense handling functions
====================
"""
from obj.obj import db
from modellib import *
from trnlib import *
from obj.flat import print_line
from obj.objtools import post_filter,print_sum
from gainloss import *
from modellib import getunitpricecur

def do_me(self,line={}):
    """me [Holding=<holding-filter>] [Transaction=<transaction-filter>] [DTSTART=<dtstart>] [DTEND=<dtend>]
"""
    transaction_filter=gettransaction_filter(line)
    expenses=db.getallobjects('Expense')
    if not expenses:
        print "There are no Expense objects, doing nothing"
        return
    for h in getholdings(line):
        # Skip no checking account
        if not h.ischecking(): continue
        trans=gettransactions(transaction_filter,h)
        if not trans: continue
        print sobj(h)
        for tran in trans:
            # Skip non-DEBIT transactions
            #if not tran.UNITS or tran.UNITS[0]>=0: continue
            #trnprint(tran)
            # Skip transactions that where already calssified
            if tran.Expense: continue
            exp=[]
            for e in expenses:
                for t in e.TransactionFILTER:
                    assert isinstance(t,dict)
                    if db.getallobjects([tran],t):
                        exp.append(e)
            exp=list(set(exp)) # remove duplicates
            if len(exp)>1:
                perr('Conflict',[tran]+exp)
            if exp:
                tran.update_tag('Expense',[exp[0]])
            else:
                pinfo('EXPCAT='+tran.sprint())

def do_re(self,line={}):
    """re [Holding=<holding-filter>] [Transaction=<transaction-filter>] [DTSTART=<dtstart>] [DTEND=<dtend>] [CURDEF=USD*]
Report expense
"""
    curdef=getcurdef(line)
    transaction_filter=gettransaction_filter(line)
    expenses=db.getallobjects('Expense')
    exptotal=GainLoss(curdef)
    for h in getholdings(line):
        # Skip no checking account
        if not h.ischecking(): continue
        trans=gettransactions(transaction_filter,h)
        if not trans: continue
        print `h`
        for tran in trans:
            # Skip non-DEBIT transactions
            if not tran.UNITS: continue
            p,cur=getunitpricecur(tran)
            total=tran.UNITS[0]*p
            #trnprint(tran)
            # Skip transactions that where already calssified
            if tran.Expense:
                expcat=tran.Expense[0].EXPCAT[0]
            else:
                expcat='sundries'
            exptotal.add(expcat,total,tran.DTSETTLE[0],cur)
    exptotal.pprint()
